package sxkyxt.kjyxgs.cn.ishop_gather_uploading_android;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;


import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ReportPhotosActivity extends TitleActivity {

    private List<ImageModel> mImageList;// 相册图片
    private LayoutInflater mLayoutInflater;
    private GridView mGridView;
    private Adapter mAdapter;
    private ImageWork mImageWork;// 图片加载类
    private Map<Integer, String> mHashMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitlesConfiguration();
        setContentView(R.layout.activity_report_photos);
        init();
    }

//	@Override
//	protected void onBackward(View backwardView) {
//		intoActivity(ReportActivity.class);
//	}

    @Override
    protected void onForward(View forwardView) {

        if (mHashMap != null) {
//			Intent into = new Intent(this, ReportActivity.class);
            Intent into = new Intent();
            into.putExtra("datas", (Serializable) mHashMap);
//			startActivity(into);
            setResult(RESULT_OK, into);
            finish();
        } else {
            showToast(getString(R.string.not_photo));
        }

    }

    private void setTitlesConfiguration() {
        setTitle(getString(R.string.select_photos));
        showBackwardView(R.string.back, true);
        showForwardView(R.string.ok, true);
    }

    @Override
    protected void onStop() {
        super.onStop();
        ActivityStack.getInstance().removeActivity(this);
        finish();
    }

    ;

//	@Override
//	public boolean dispatchKeyEvent(KeyEvent event) {
//
//		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
//				&& event.getAction() == KeyEvent.ACTION_UP) {
//
//			intoActivity(ReportActivity.class);
//
//			return true;
//		}
//
//		return super.dispatchKeyEvent(event);
//	}

    void init() {

        mImageWork = new ImageWork(this);
        mLayoutInflater = LayoutInflater.from(this);
        mImageList = PhotoUtils.getImages(this);
        mGridView = (GridView) findViewById(R.id.gv_main);
        mAdapter = new Adapter();
        mGridView.setAdapter(mAdapter);
        mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView,
                                             int scrollState) {
                // Pause fetcher to ensure smoother scrolling when flinging
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                    // Before Honeycomb pause image loading on scroll to help
                    // with performance
                    if (!PhotoUtils.hasHoneycomb()) {
                        mImageWork.setPauseWork(true);
                    }
                } else {
                    mImageWork.setPauseWork(false);
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
            }
        });
    }

    private class Adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mImageList.size();
        }

        @Override
        public Object getItem(int i) {
            return mImageList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @SuppressLint("UseSparseArrays")
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            final ImageModel imageModel = (ImageModel) getItem(i);
            final String path = imageModel.getPath();
            boolean checked = imageModel.getIsChecked();
            mHashMap = new HashMap<Integer, String>();
            if (view == null) {
                viewHolder = new ViewHolder();
                view = mLayoutInflater.inflate(R.layout.imageview, null);
                viewHolder.imageView = (ImageView) view
                        .findViewById(R.id.gv_image);
                viewHolder.checkBox = (CheckBox) view
                        .findViewById(R.id.cb_imageview);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.checkBox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton arg0,
                                                     boolean arg1) {
                            // Toast.makeText(MainActivity.this, "1212;"+path,
                            // 3).show();TODO

                            imageModel.setIsChecked(arg1);
                            // 如果选择中 cheked 为true 添加进 listpath 里 , 如果取消 就删除
                            // mHashMap
                            if (imageModel.getIsChecked()) {
                                mHashMap.put(
                                        Integer.decode(imageModel.getId()),
                                        imageModel.getPath());
                            } else {
                                mHashMap.remove(Integer.getInteger(imageModel
                                        .getId()));
                            }

                        }
                    });
            viewHolder.checkBox.setChecked(checked);
            mImageWork.loadImage(path, viewHolder.imageView);
            return view;
        }

        class ViewHolder {
            ImageView imageView;
            CheckBox checkBox;
        }
    }

}
